package com.polytech.ihm.factory;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Label;

import javax.swing.GroupLayout.Alignment;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class LabledComponent extends JPanel {
	protected JComponent component;
	protected JLabel label;
	protected JPanel pan=new JPanel();
	private int labelWitdh=0;
	public static final int VERTICAL=0;
	public static final int HORIZONTAL=0;
 
  	public LabledComponent(String caption,JComponent component,int labelWidth) {
		setLayout(new FlowLayout(FlowLayout.LEFT,10,2));
		if(!caption.endsWith(":"))caption+=" : ";
		this.labelWitdh=labelWidth;
		label=new JLabel(caption);
		if(labelWidth==0){
			setLabelWitdh((int) label.getPreferredSize().getWidth());
		}
		this.component=component;
		add(label);
		add(component);
		component.setFont(new Font("Arial",Font.BOLD,13));
  	}
  	public LabledComponent(String caption,JComponent component) {
		this(caption, component,0);
  	}
  	public LabledComponent(String caption,JComponent component,int alignement,int labelWith) {
	  if(alignement==VERTICAL){
		  setLayout(new FlowLayout(FlowLayout.LEFT,10,10));
		  pan.setLayout(new BorderLayout());
		  if(!caption.endsWith(":"))caption+=" : ";
			label=new JLabel(caption);
			this.component=component;
			pan.add(label,BorderLayout.NORTH);
			pan.add(component,BorderLayout.CENTER);
			add(pan);
	  }
	  
  	}
  	public void setComponentPreferdSize(Dimension dim){
	  component.setPreferredSize(dim);
  	}
  	public void enable(boolean etat){
	  this.component.enable(etat);
  	}
	public JComponent getComponent() {
		return component;
	}
	public void setComponent(JComponent component) {
		this.component = component;
	}
	public int getLabelWitdh() {
		return labelWitdh;
	}
	public void setLabelWitdh(int labelWitdh) {
		this.labelWitdh = labelWitdh;
		label.setPreferredSize(new Dimension(labelWitdh,label.getPreferredSize().height));
	}
}
